﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using DEMO3.BUS;
using DEMO3.DTO;

namespace DEMO3
{
    public partial class ThanhVien : UserControl
    {
        public ThanhVien()
        {
            InitializeComponent();
        }

        private void ThanhVien_Load(object sender, EventArgs e)
        {
            DataProvider test = new DataProvider();
            if (!test.OpenConnect())
                Main.Warning();
            else
               LoadData();
            test.CloseConnect();
        }
        private void LoadData()
        {
            ThanhVienBUS tvbus = new ThanhVienBUS();
            gc.DataSource = tvbus.Get();

            DataProvider test = new DataProvider();
            cbxLoaiTV.DataSource = test.ExcuteQuery("GET_LOAITHANHVIEN", null);
            cbxLoaiTV.DisplayMember = "TenLoai";
            cbxLoaiTV.ValueMember = "MaLoaiTV";
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            ThanhVienDTO tvdto = new ThanhVienDTO();
            tvdto.TenThanhVien = txtTenThanhVien.Text;
            tvdto.LoaiThanhVien = (string)cbxLoaiTV.SelectedValue;
            ThanhVienBUS tvbus = new ThanhVienBUS();
            tvbus.Add(tvdto);
            LoadData();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            ThanhVienDTO tvdto = new ThanhVienDTO();
            tvdto.MaThanhVien = (int)gv.GetRowCellValue(gv.FocusedRowHandle, "MaThanhVien");
            ThanhVienBUS tvbus = new ThanhVienBUS();
            tvbus.Delete(tvdto);
            LoadData();
        }

        private void btCapNhat_Click(object sender, EventArgs e)
        {
            ThanhVienDTO tvdto = new ThanhVienDTO();
            tvdto.TenThanhVien = txtTenThanhVien.Text;
            tvdto.LoaiThanhVien = (string)cbxLoaiTV.SelectedValue;
            tvdto.MaThanhVien = (int)gv.GetRowCellValue(gv.FocusedRowHandle, "MaThanhVien");
            ThanhVienBUS tvbus = new ThanhVienBUS();
            tvbus.Update(tvdto);
            LoadData();
        }

        private void gv_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gv.FocusedRowHandle != gv.RowCount && gv.RowCount != 0 && gv.FocusedRowHandle>-1)
            {
                txtTenThanhVien.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "TenThanhVien").ToString();
                cbxLoaiTV.SelectedValue = gv.GetRowCellValue(gv.FocusedRowHandle, "LoaiThanhVien").ToString();
                txtID.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "ID").ToString();
                txtPass.Text = gv.GetRowCellValue(gv.FocusedRowHandle, "Pass").ToString();
            }
        }
    }
}
